package Dao.impl;

import java.util.List;

import javax.persistence.Query;

import Bean.Mecanico;
import Bean.TipoItem;
import Dao.interfaces.IMecanicoDao;
import exception.CrudException;

public class MecanicoDaoImpl extends GenericCrud<Mecanico> implements IMecanicoDao {

	@Override
	public void excluir(Long id) throws CrudException {
		excluir(Mecanico.class, id);
	}
	
	@Override
	public Mecanico carregar(Long id) throws CrudException {
		return carregar(Mecanico.class, id);
	}

	@Override
	public List<Mecanico> carregarTodos() throws CrudException {
		return carregarTodos(Mecanico.class);
	}

	@Override
	public List<Mecanico> carregarPorNome(String nome, int status) throws CrudException {
		String query = "SELECT o FROM Mecanico o where o.nome like '%" + nome + "%'";
		if (status != 3)
			query += "and o.ativo = :status";
		em = emf.createEntityManager();	
		List<Mecanico> lista;
		Query query2 = em.createQuery(query);
		if (status == 1)
			query2.setParameter("status", true);
		else if (status == 2)
			query2.setParameter("status", false);
		lista = query2.getResultList();
		em.close();
		return lista;
	}
}
